const fs = require("fs");
const path = require("path");
const curd = require("../../utils/curd");

function getAllJsFiles(folderPath, result = []) {
    const items = fs.readdirSync(folderPath);
    items.forEach(item => {
        const itemPath = path.join(folderPath, item);
        const stats = fs.statSync(itemPath);
        if (stats.isDirectory()) {
            getAllJsFiles(itemPath, result); // 递归处理子文件夹
        } else if (stats.isFile() && item.endsWith('.js')) {
            result.push(itemPath); // 存储符合条件的文件路径
        }
    });
    return result.filter(item => item != __filename);
}
module.exports = {
    install: async app => {
        var arr = getAllJsFiles(__dirname);
        arr.forEach(async (item,index)=>{
            const router = require(item);
            const fileName = path.relative(__dirname, item).replaceAll("\\","/");
            await curd.install(router);
            app.use(`/${fileName.replace(/\.[^.]+$/, "")}`,router);
        });
        return;
    }
}